package com.top10.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.apache.commons.lang.StringUtils;


import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author sny
 * @Package com.top10.utils
 * @date 2018/10/8
 */
public class JsonUtils {


//    public static List jsonToList(String json, Class beanClass) {
//        if (!StringUtils.isBlank(json)) {
//
//            return JSONObject.parseArray(json, beanClass);
//        } else {
//            return null;
//
//        }
//    }
//
//    //List集合转换为json
//    public static JSON listToJson(List list) {
//        JSON json=(JSON) JSON.toJSON(list);
//        return json;
//    }



    public static String parseObjToJson(Object object) {
        String string = null;
        try {
            //string = JSON.toJSONString(object);
            string = JSONObject.toJSONString(object);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return string;
    }





    public static <T> T parseJsonToObj(String json, Class<T> c) {
        try {

            JSONObject jsonObject = JSON.parseObject(json);
            return JSON.toJavaObject(jsonObject, c);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }



    public static <T> String listToJson(List<T> ts) {
        String jsons = JSON.toJSONString(ts);
        return jsons;
    }


     public static <T> List<T> jsonToList(String jsonString, Class<T> clazz) {
         @SuppressWarnings("unchecked")
        List<T> ts = (List<T>) JSONArray.parseArray(jsonString, clazz);
        return ts;
    }

//            public static void main(String[] args) {
//
//
//
////               OrderVo order=getorderVo();
////            String Json = JsonUtils.parseObjToJson(order);
////           System.out.println(Json);
////            OrderVo FromJson = JsonUtils.parseJsonToObj(studentJson, OrderVo.class);
////            System.out.println(FromJson);
////            for(OrderDetail goods:FromJson.getGoodlist()){
////                System.out.println(goods.getGooduuid());
////                System.out.println(goods.getGoodsname());
////            }
//
//
//                List<OrderVo> orderVos = getorderVo2();
//                for (OrderVo order : orderVos) {
//                    String Json = JsonUtils.parseObjToJson(order);
//                     // System.out.println(studentJson);
//                     OrderVo  FromJson = JsonUtils.parseJsonToObj(Json, OrderVo.class);
//                     System.out.println(FromJson);
////                    for (OrderDetail goods : FromJson.getGoodlist()) {
////                        System.out.println(goods.getGooduuid());
////                        System.out.println(goods.getGoodsname());
////                    }
//
//                }
//
//            }

    public static void main(String[] args) {

//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//
//
//       Date date=new Date();
//
//
//       System.out.println(new SimpleDateFormat("yyyyMMdd").format(new Date())+RandomUtils.makeRandomDigit6());
//
       //List<OrderList>  orderLists= getorderlist();
        List<orderList> orderLists=  get3orderlist();

        String string=JsonUtils.listToJson(orderLists);
        System.out.println(string);

        Gson gson = new Gson();
       List<orderList> orderlist = gson.fromJson(string, new TypeToken<List<orderList>>(){}.getType());

       for(orderList order:orderlist){
//
        System.out.println(order.getOrder());
//            System.out.println(order.getAddress());
//            for(OrderDetail orderDetail:order.getGoodlist())
//            {
//                System.out.print(orderDetail.getGoodsname());
//                System.out.print(orderDetail.getGooduuid());
//                System.out.print(orderDetail.getPrice());
//                System.out.println();
//
//            }
        }

//        DecimalFormat decimalFormat = new DecimalFormat("#.00");
//        double subtotal = Double.parseDouble(decimalFormat.format(1.11 * 1.11));
//        System.out.println(subtotal);



    }

       public static List<orderList> get3orderlist(){

           OrderDetail orderDetail1=new OrderDetail("gooduuid1","goodsname1","price1");

           OrderDetail orderDetail2=new OrderDetail("gooduuid2","goodsname2","price2");
           OrderDetail orderDetail3=new OrderDetail("gooduuid3","goodsname3","price3");

           List<OrderDetail> orderDetails=new ArrayList();
            List<OrderDetail> orderDetails2=new ArrayList();
           orderDetails.add(orderDetail1);
           //orderDetails.add(orderDetail2);
           orderDetails2.add(orderDetail3);


           OrderVo orderVo1=new OrderVo("商店1","地址1",orderDetails);
           OrderVo orderVo2=new OrderVo("商店2","地址2",orderDetails2);
           List<OrderVo> order=new ArrayList();
           List<OrderVo> order2=new ArrayList();
          // List<OrderVo> order2=new ArrayList();
           order.add(orderVo1);
           order2.add(orderVo2);

           List<orderList> orderLists=new ArrayList();;
           orderList orderList=new orderList(order);
           orderList orderList1=new orderList(order2);
           orderLists.add(orderList);
           orderLists.add(orderList1);
           return orderLists;

       }


       public static OrderVo getorderVo(){

           OrderDetail orderDetail1=new OrderDetail("gooduuid1","goodsname1","price1");

           OrderDetail orderDetail2=new OrderDetail("gooduuid2","goodsname2","price2");
           OrderDetail orderDetail3=new OrderDetail("gooduuid3","goodsname3","price3");

           List<OrderDetail> orderDetails=new ArrayList();
          // List<OrderDetail> orderDetails2=new ArrayList();
           orderDetails.add(orderDetail1);
           orderDetails.add(orderDetail2);
           //orderDetails2.add(orderDetail3);

           OrderVo orderVo=new OrderVo("商店1","地址1",orderDetails);
         //  OrderVo orderVo2=new OrderVo("商店2","地址2",orderDetails2);

           return orderVo;
         //  List<OrderVo> orderVos=new ArrayList();
         //  orderVos.add(orderVo);
          // orderVos.add(orderVo2);
         //  return orderVos;

       }


    public static List<OrderVo> getorderVo2(){

        OrderDetail orderDetail1=new OrderDetail("gooduuid1","goodsname1","price1");

        OrderDetail orderDetail2=new OrderDetail("gooduuid2","goodsname2","price2");
        OrderDetail orderDetail3=new OrderDetail("gooduuid3","goodsname3","price3");

        List<OrderDetail> orderDetails=new ArrayList();
         List<OrderDetail> orderDetails2=new ArrayList();
        orderDetails.add(orderDetail1);
        orderDetails.add(orderDetail2);
        orderDetails2.add(orderDetail3);

        OrderVo orderVo=new OrderVo("商店1","地址1",orderDetails);
          OrderVo orderVo2=new OrderVo("商店2","地址2",orderDetails2);


          List<OrderVo> orderVos=new ArrayList();
          orderVos.add(orderVo);
          orderVos.add(orderVo2);
          return orderVos;

    }



}
